package com.ug_project.system

import java.text.SimpleDateFormat
import java.util.*

/**
 * Created by yeganaaa on 3/9/2017.
 */

val defaultPattern = "yyyy-MM-dd HH:mm:ss"
val patternDate = "yyyy-MM-dd"

var Calendar.year: Int
    get() = this.get(Calendar.YEAR)
    set(value) {
        this.set(Calendar.YEAR, value)
    }

var Calendar.month: Int
    get() = this.get(Calendar.MONTH) + 1
    set(value) {
        this.set(Calendar.MONTH, value - 1)
    }

var Calendar.day: Int
    get() = this.get(Calendar.DAY_OF_MONTH)
    set(value) {
        this.set(Calendar.DAY_OF_MONTH, value)
    }

var Calendar.hour: Int
    get() = this.get(Calendar.HOUR_OF_DAY)
    set(value) {
        this.set(Calendar.HOUR_OF_DAY, value)
    }

var Calendar.minute: Int
    get() = this.get(Calendar.MINUTE)
    set(value) {
        this.set(Calendar.MINUTE, value)
    }

var Calendar.second: Int
    get() = this.get(Calendar.SECOND)
    set(value) {
        this.set(Calendar.SECOND, value)
    }

fun createCalendar(Year: Int = 1964, Month: Int = 4, Day: Int = 6, Hour: Int = 0, Minute: Int = 0, Second: Int = 0): Calendar {
    var calen = Calendar.getInstance()
    calen.year = Year
    calen.month = Month
    calen.day = Day
    calen.hour = Hour
    calen.minute = Minute
    calen.second = Second
    return calen
}

/*نۆۋەتتىكى كالىندار*/
val nowCalendar: Calendar
    get() = Calendar.getInstance()

fun Calendar.toString(pattern: String = defaultPattern): String {
    return this.time.toString(pattern)
}


/*
نۆۋەتتىكى ۋاقىتنى ئوقۇش
 */
fun nowTimeString(pattern: String = defaultPattern): String {
    var sdf = SimpleDateFormat(pattern)
    return sdf.format(nowTime)
}

/*
C# چىقارغان Json دىن ۋاقىتقا ئىرىشىش
 */
fun String.toTimeFromTimesTampStirng(): Calendar {
    var stime = this
    if (this.contains("T"))
        stime = this.replace("T", " ")
    var sdf = SimpleDateFormat(defaultPattern)
    return sdf.parse(stime).toCalendar()
}

fun String.toTimeFromTimesTampStirng(pattern: String = defaultPattern): Calendar {
    var stime = this
    if (this.contains("T"))
        stime = this.replace("T", " ")
    return try {
        var sdf = SimpleDateFormat(pattern)
        sdf.parse(stime).toCalendar()
    } catch (e: Exception) {
//        throw Exception("this is err")
        Calendar.getInstance()
    }
}

val nowTime: Date
    get() = Date(System.currentTimeMillis())

fun Date.toString(pattern: String = defaultPattern): String {
    var sdf = SimpleDateFormat(pattern)
    return sdf.format(this)
}

fun Date.toCalendar(): Calendar {
    var calendar = nowCalendar
    calendar.time = this
    return calendar
}

/*نۆۋەتتىكى يىل*/
val nowYear: Int
    get() = nowCalendar.year
/*نۆۋەتتىكى ئاي*/
val nowMonth: Int
    get() = nowCalendar.month
/*نۆۋەتتىكى كۈن*/
val nowDay: Int
    get() = nowCalendar.day
/*نۆۋەتتىكى سائەت*/
val nowHour: Int
    get() = nowCalendar.hour
/*نۆۋەتتىكى مىنۇت*/
val nowMinute: Int
    get() = nowCalendar.minute
/*نۆۋەتتىكى سىكۇنىت*/
val nowSecond: Int
    get() = nowCalendar.second
/*نۆۋەتتىكى چۈشتىن بۇرۇن-چۈشتىن كىيىن*/
val nowAMPM: Int
    get() = nowCalendar.get(Calendar.AM_PM)

/*String Extensions*/

fun String.toDate(pattern: String): Date {
    val sdf = SimpleDateFormat(pattern)
    return sdf.parse(this)
}

fun String.toCalendar(pattern: String): Calendar {
    val sdf = SimpleDateFormat(pattern)
    return sdf.parse(this).toCalendar()
}